package top.infopub.mgr.la.service.impl;


import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.la.dao.LawcaseInfoAttDao;
import top.infopub.mgr.la.dao.LawcaseInfoDao;
import top.infopub.mgr.la.dao.LawcaseInfoMultiDao;
import top.infopub.mgr.la.domain.LawcaseInfoDomain;
import top.infopub.mgr.la.service.LawcaseInfoService;
import top.infopub.mgr.la.vo.LawcaseInfoVo;
import top.infopub.model.la.LawcaseInfo;
import top.infopub.model.la.LawcaseInfoAtt;
import top.infopub.model.la.LawcaseInfoMulti;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class LawcaseInfoServiceImpl extends GenericServiceImpl<LawcaseInfo, String> implements LawcaseInfoService {

    @Autowired
    private LawcaseInfoDao lawcaseInfoDao;

    @Autowired
    private LawcaseInfoAttDao lawcaseInfoAttDao;

    @Autowired
    private LawcaseInfoMultiDao lawcaseInfoMultiDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<LawcaseInfo, String> getDao() {
        return lawcaseInfoDao;
    }

    @Override
    public Page<LawcaseInfoDomain> selectByDomainWhere(Page<LawcaseInfoDomain> page,
                                                       LawcaseInfoVo lawcaseInfoVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<LawcaseInfoDomain>)lawcaseInfoDao.selectByDomainWhere(lawcaseInfoVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveLawcaseInfo(LawcaseInfo lawcaseInfo, List<LawcaseInfoMulti> lawcaseInfoMultis,
                                List<LawcaseInfoAtt> lawcaseInfoAtts) {

        lawcaseInfoDao.insertById(lawcaseInfo);

        if (CollectionUtils.isNotEmpty(lawcaseInfoMultis)) {
            lawcaseInfoMultiDao.insertByBatch(lawcaseInfoMultis);
        }

        if (CollectionUtils.isNotEmpty(lawcaseInfoAtts)) {
            lawcaseInfoAttDao.insertByBatch(lawcaseInfoAtts);
        }

        //FlowTemplateEnum templateEnum = FlowTemplateEnum.LA_LAWCASE_INFO;
        //wsFlowProcess.launch(templateEnum, lawcaseInfo.getId(), lawcaseInfo.getProcessId());

    }

    @Override
    public LawcaseInfoVo queryById(String id) {
        return lawcaseInfoDao.queryById(id);
    }

}